package com.financing.wap.web.controller.adminapi;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.DstPosterDTO;
import com.financing.wap.service.IDstPosterService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("adminDstPosterController")
@RequestMapping("/adminapi/dstPoster")
public class DstPosterController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstPosterService dstPosterService;
	
	@RequestMapping("/listDstPoster")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, DstPosterDTO dstPoster,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query dstPoster list begin .......");
			
			dstPoster.setBegin(pager.getStartIndex());
			dstPoster.setPageSize(pager.getPageSize());
            dstPoster.setDeleteTag("1");
			
			int count = dstPosterService.queryListCount(dstPoster);
			
			pager.setTotalCount(count);
			
			List<DstPosterDTO> dstPosterList = null;
			
			if(count > 0){
				dstPosterList = dstPosterService.queryList(dstPoster);
			}else{
				dstPosterList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", dstPosterList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query dstPoster list end .......");
		}catch(Exception e){
			log.error("query dstPoster error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addDstPoster")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, DstPosterDTO dstPoster){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add dstPoster  begin .......");
			
			DTOdateSetUtil.setCreateInfo(dstPoster);
			DTOdateSetUtil.setUpdateInfo(dstPoster);
			dstPoster.setPosterStatus("0");
			
			int id = dstPosterService.insert(dstPoster);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add dstPoster list end .......");
		}catch(Exception e){
			log.error("add dstPoster error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailDstPoster")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, DstPosterDTO dstPoster){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail dstPoster  begin .......");
			
			Integer id = dstPoster.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DstPosterDTO dto = dstPosterService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail dstPoster  end .......");
		}catch(Exception e){
			log.error("query detail dstPoster error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteDstPoster")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, DstPosterDTO dstPoster){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete dstPoster  begin .......");
			
			Integer id = dstPoster.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			dstPosterService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete dstPoster  end .......");
		}catch(Exception e){
			log.error("delete dstPoster error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateDstPoster")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, DstPosterDTO dstPoster){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update dstPoster  begin .......");
			
			Integer id = dstPoster.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(dstPoster);
			
			dstPosterService.update(dstPoster);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update dstPoster list end .......");
		}catch(Exception e){
			log.error("update dstPoster error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceDstPoster")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, DstPosterDTO dstPoster){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce dstPoster  begin .......");
			
			Integer id = dstPoster.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(dstPoster);
			
			dstPosterService.updateForce(dstPoster);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce dstPoster list end .......");
		}catch(Exception e){
			log.error("updateForce dstPoster error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
